package com.xyht.sca_s.student_manage_system.modules.org.util;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xyht.sca_s.student_manage_system.modules.org.entity.SmsOrgStructure;
import com.xyht.sca_s.student_manage_system.modules.org.entity.SmsOrgUserRelation;
import com.xyht.sca_s.student_manage_system.modules.org.mapper.SmsOrgStructureMapper;
import com.xyht.sca_s.student_manage_system.modules.org.mapper.SmsOrgUserRelationMapper;
import com.xyht.sca_s.student_manage_system.modules.user.entity.SmsRole;
import com.xyht.sca_s.student_manage_system.modules.user.entity.SmsUser;
import com.xyht.sca_s.student_manage_system.modules.user.entity.SmsUserRoleRelation;
import com.xyht.sca_s.student_manage_system.modules.user.mapper.SmsRoleMapper;
import com.xyht.sca_s.student_manage_system.modules.user.mapper.SmsUserMapper;
import com.xyht.sca_s.student_manage_system.modules.user.mapper.SmsUserRoleRelationMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.stream.Collectors;

import static com.xyht.sca_s.student_manage_system.modules.org.constants.OrgTypeConstant.ORG_TYPE_CLASS;
import static com.xyht.sca_s.student_manage_system.modules.user.constant.TeacherAndStudentFlag.TEACHER_FLAG;

@Component
public class CommonUtil {
    @Autowired
    private SmsRoleMapper smsRoleMapper;
    @Autowired
    private SmsOrgStructureMapper smsOrgStructureMapper;
    @Autowired
    private SmsOrgUserRelationMapper smsOrgUserRelationMapper;
    @Autowired
    private SmsUserMapper smsUserMapper;
    @Autowired
    private SmsUserRoleRelationMapper smsUserRoleRelationMapper;

    //获取当前班级辅导员
    public List<SmsUser> findInstructorByClassID(Integer org_id) {
        try {
            //班级组织有效性
            SmsOrgStructure smsOrgStructure = smsOrgStructureMapper.selectOne(new QueryWrapper<SmsOrgStructure>()
                    .lambda()
                    .eq(SmsOrgStructure::getId, org_id)
                    .eq(SmsOrgStructure::getLevel, ORG_TYPE_CLASS));
            if (smsOrgStructure == null || smsOrgStructure.getId() == null) {
                return null;
            }

            //辅导员角色有效性
            SmsRole smsRole = smsRoleMapper.selectOne(new QueryWrapper<SmsRole>()
                    .lambda()
                    .eq(SmsRole::getRoleName, "辅导员"));
            if (smsRole == null || smsRole.getId() == null) {
                return null;
            }

            List<String> orgUserId = smsOrgUserRelationMapper.selectList(new QueryWrapper<SmsOrgUserRelation>()
                    .lambda()
                    .eq(SmsOrgUserRelation::getOrgId, org_id))
                    .stream()
                    .map(SmsOrgUserRelation::getUserId)
                    .collect(Collectors.toList());
            if (orgUserId.size() < 1) {
                return null;
            }
            //当前班级中角色有“辅导员”的老师集合
            List<SmsUser> userList = smsUserMapper.selectList(new QueryWrapper<SmsUser>()
                    .lambda()
                    .in(SmsUser::getId, orgUserId)
                    .eq(SmsUser::getFlag, TEACHER_FLAG))
                    .stream()
                    .filter(smsUser -> {
                        List<SmsUserRoleRelation> list = smsUserRoleRelationMapper.selectList(new QueryWrapper<SmsUserRoleRelation>()
                                .lambda()
                                .eq(SmsUserRoleRelation::getUserId, smsUser.getId())
                                .eq(SmsUserRoleRelation::getRoleId, smsRole.getId()));
                        if (list == null || list.size() < 1) {
                            return false;
                        }
                        return true;
                    })
                    .collect(Collectors.toList());
            if (userList.size() < 1) {
                return null;
            }
            return userList;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    //获取当前用户班级
    public SmsOrgStructure getUserOrgClass(String user_id) {
        try {
            List<String> orgIDList = smsOrgUserRelationMapper.selectList(new QueryWrapper<SmsOrgUserRelation>()
                    .lambda()
                    .eq(SmsOrgUserRelation::getUserId, user_id))
                    .stream()
                    .map(SmsOrgUserRelation::getOrgId)
                    .collect(Collectors.toList());

            if (orgIDList.size() > 0) {
                for (String orgID : orgIDList) {
                    SmsOrgStructure smsOrgStructure = smsOrgStructureMapper.selectById(orgID);
                    if (Integer.parseInt(smsOrgStructure.getLevel()) == ORG_TYPE_CLASS) {
                        return smsOrgStructure;
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


}
